<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title></title>
<link rel="stylesheet" href="../css/reset.css" type="text/css" />
<link rel="stylesheet" href="../css/prettify.css" type="text/css" />
<script type="text/javascript" src="../js/prettify.js"></script>

</head>
<body onload="prettyPrint()">

  <h1>用PHP操作 SQLite 数据库</h1>
  <hr />
  <p>SQLite 是个小巧方便的数据库，它相对于其他数据库最大的区别在于整个库只有1个文件以及开源、免费。</p>
  <p>常用的 SQLite 数据库有两个版本，sqlite2和sqlite3，用PHP处理的方法不同。</p>
  <h3>sqlite2</h3>
  <p>sqlite2被php5的原生支持</p>
 <pre class="prettyprint linenums">
&lt;?php
// set access parameters
$db = "goods.db3";

$conn= sqlite_open($db) or die ("ERROR: Cannot open database");

// sqlite_query($conn, "CREATE TABLE employees (ID INTEGER NOT NULL PRIMARY KEY, Name VARCHAR(255));");
sqlite_query($conn, "INSERT INTO employees (Name) VALUES ('James Fisher');");
sqlite_query($conn, "INSERT INTO employees (Name) VALUES ('Peter Hutchinson');");
sqlite_query($conn, "INSERT INTO employees (Name) VALUES ('Richard Hartis');");

$sql ="select * from employees";
$result = sqlite_query($conn, $sql) or die("Error in query execution: " . sqlite_error_string(sqlite_last_error($conn)));
if(sqlite_num_rows($result) > 0)
{
	while($row=sqlite_fetch_array($result)){
		echo $row[0] . " (" . $row[1] . ") ";
}
}
sqlite_close($conn);
?&gt;

</pre>
<h3>sqlite3</h3>
<p>当前使用php操作sqlite3需要启用sqlite pdo，在未来的php6中，sqlite3将被原生支持。</p>
<p>用php操作sqlite3</p>
<pre class="prettyprint linenums">

&lt;?php
$dsn = 'sqlite:wolf.sq3';
try{
      $dbh = new pdo($dsn);
      echo 'PDO Connection OK!'.'&lt;br /&gt;';
      // $dbh->exec("create table zzu(id integer,name varchar(255))");
      // echo 'Create Table Ok!'.'&lt;br /&gt;';
      // $dbh->exec("insert into zzu values(1,'usen')");
      // echo 'Insert Data OK!'.'&lt;br /&gt;';
      $dbh->beginTransaction();
      $sth = $dbh->prepare('select * from page');
      $sth->execute();
      $result = $sth->fetchAll();
      print_r($result);
      $dsn = null;

}catch (pdoexception $e){
     echo 'Connection failed:'.$e->getmessage();
     $dsn = null;
}
?&gt;

</pre>

  </body>
</html>
